﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Mapping Application Block
//===============================================================================

using System.Collections.Generic;
using System.Windows;
using EntLibContrib.Mapping.QuickStart.DomainObjects;
using Messages = EntLibContrib.Mapping.QuickStart.Properties.Resources;

namespace EntLibContrib.Mapping.QuickStart
{
	/// <summary>
	/// Interaction logic for App.xaml
	/// </summary>
	public partial class App : Application
	{
		#region Fields
		private ICollection<Basic1> basic1Collection;
		#endregion

		#region Properties
		/// <summary>
		/// Gets the basic1 collection.
		/// </summary>
		/// <value>The basic1 collection.</value>
		public ICollection<Basic1> Basic1Collection
		{
			get
			{
				if (basic1Collection == null)
				{
					basic1Collection = DomainObjectController.GetBasic1Collection();
				}

				return basic1Collection;
			}
		}

		/// <summary>
		/// Gets a value indicating whether data has changed and needs to be saved away before exit.
		/// </summary>
		/// <value><c>true</c> if this instance is dirty; otherwise, <c>false</c>.</value>
		public bool IsDirty
		{
			get { return DomainObjectController.IsDirtyBasic1Collection(Basic1Collection); }
		}
		#endregion

		#region Protected Event Handlers
		/// <summary>
		/// Raises the <see cref="E:System.Windows.Application.SessionEnding"/> event.
		/// </summary>
		/// <param name="e">A <see cref="T:System.Windows.SessionEndingCancelEventArgs"/> that contains the event data.</param>
		protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
		{
			base.OnSessionEnding(e);

			e.Cancel = !AllowShutdown();
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Checks if the shutdown is allowed.
		/// </summary>
		/// <returns>true if shutdown is allowed, false if data saves are outstanding</returns>
		public bool AllowShutdown()
		{
			bool allowShutdown = true;

			if (IsDirty)
			{
				MessageBoxResult exitResult = MessageBox.Show(Messages.ExitMessage, Messages.ExitHeader, MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.Yes);
				if (exitResult == MessageBoxResult.No)
					allowShutdown = false;
			}

			return allowShutdown;
		}

		/// <summary>
		/// Refreshes this instance.
		/// </summary>
		public void Refresh()
		{
			basic1Collection = DomainObjectController.GetBasic1Collection();
		}
		#endregion
	}
}
